import { deepCopy, uniqueId } from '@/utils/index'
import { cEval } from '@/utils/index'
// 添加一些数据流中的方法
const myMixin = {
  data() {
    return {

    }
  },
  methods: {
    // 验证输入框内容
    validateInput(value) {
      try {
        if (!isNaN(value)) {
          throw new Error()
        } else {
          return { state: 'success', type: 'object', value: JSON.parse(value) }
        }
      } catch (err1) {
        if (typeof cEval(value) === 'object') {
          return { state: 'failed', type: 'operation', value: cEval(value).error }
        } else {
          return { state: 'success', type: 'operation', value: cEval(value) }
        }
      }
    },
    // 执行输入框
    executeInput() {
      const res = this.validateInput(this.inputValue)
      // console.log(res)
      if (res.type === 'object') {
        if (res.value.name) {
          // 切分数据集
          if (res.value.name === 'splitFrame') {
            this.tosplitFrame(res)
          }
        } else {
          this.toFailPage2()
        }
      }
      if (res.state === 'success' && res.type === 'operation') {
        this.toSuccPage(res)
      }
      if (res.state === 'failed' && res.type === 'operation') {
        this.toFailPage(res)
      }
    },
    // 跳转到新页面
    toNewPage(inputValue, type, data) {
      const cloneHomeData = deepCopy(this.flow.data)
      cloneHomeData.forEach((item, index) => {
        item.active = false
        if (item.id === this.selFlowData.id) {
          cloneHomeData.splice(index, 1)
        }
      })

      cloneHomeData.push({
        type,
        active: true,
        id: uniqueId(),
        data: { type: 'cs', input: `${inputValue}`, resultData: data }
      })
      this.$store.dispatch('flow/updateHomeData', cloneHomeData)
    },
    // 跳转到成功页面
    toSuccPage(res) {
      this.toNewPage(this.inputValue, 'runSucc', res.value)
    },
    // 跳转到失败页面
    toFailPage(res) {
      this.toNewPage(this.inputValue, 'runErr', res.value.toString())
    },
    // 跳转到失败页面并提示参数有误
    toFailPage2() {
      this.toNewPage(this.inputValue, 'runErr', '参数格式有误，请使用 {"name":"","param":""} 的格式')
    },
    // 跳转到切分数据集页面
    tosplitFrame(res) {
      this.toNewPage(this.inputValue, 'splitFrame', res.value)
    }
  }
}
export default myMixin

